package com.jprocms.module.cms.event;

import com.jpro.module.system.api.permission.PermissionApi;
import com.jpro.module.system.api.permission.dto.ChannelCreateReqVO;
import com.jprocms.module.cms.dal.dataobject.channel.ChannelDO;
import com.jprocms.module.cms.dal.mysql.channel.ChannelMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * {@link ChannelEvent} 的监听器
 * 实现栏目创建自动赋予角色数据 权限的栏目和内容的
 *
 * @author jprocms
 */
@Slf4j
@Component
public class ChannelRolePermListener implements ApplicationListener<ChannelEvent> {

    @Resource
    private ChannelMapper channelMapper;
    @Resource
    private PermissionApi permissionApi;

    @Override
    public void onApplicationEvent(ChannelEvent event) {
        if (event.getId() != null) {
            ChannelDO channelDO = channelMapper.selectById(event.getId());
            if (channelDO == null) {
                return;
            }
            permissionApi.assignRoleNewChannelScope(new ChannelCreateReqVO(event.getId(), event.getParentId(), event.getSiteId()));
        }
    }
}
